<template>
    <div>

        <uni-forms
                :model="checkAdmin"
        >

            <div class="cu-form-group">
                <div class="title">账号</div>
                <input v-model="checkAdmin.id" type="text"/>
            </div>


            <div class="cu-form-group">
                <div class="title">密码</div>
                <input v-model="checkAdmin.password" type="password"/>
            </div>

            <div class="cu-form-group">
                <div class="title">验证码</div>
                <input v-model="checkAdmin.mailCode" type="text"/>
                <button v-if="isSend" class="cu-btn bg-green shadow" disabled="ture">{{ countdownTime }}s</button>
                <button v-else class="cu-btn bg-green shadow" @click="sendMailCode()">验证码</button>
            </div>
            <div class="padding flex flex-direction">
                <button :plain="true" type="primary" @click="submit()" class="cu-btn bg lg">登录</button>
                <button form-type="reset" class="cu-btn bg lg">重置</button>
            </div>
        </uni-forms>


    </div>
</template>

<script setup>
import request from "../../utils/request";
import md5 from "js-md5"
import {reactive, ref} from "vue";
import uniapp from "@dcloudio/uni-cli-shared/dist/postcss/plugins/uniapp";


const checkAdmin = reactive({
    id: '',
    password: '',
    mailCode: ''
})
const userMain = reactive({
    id: '',
    tel: '',
    mail: '',
    storeCount: 0,
    openId: ''

})
const isSend = ref(false)
const countdownTime = ref(60)


const sendMailCode = () => {
    if (checkAdmin.id === '' || checkAdmin.password === '') {
        uni.showToast({
            title: "检查账号或密码是否为空",
            icon: "error",
            duration: 2000
        })
    } else {
        request.post("/store/loginUserOnSendMail", checkAdmin).then(res => {
            if (res.code === 200) {
                isSend.value = true
                countdownTime.value = 60
                uni.showToast({
                    title: "已向绑定邮箱发送验证码",
                    icon: "none",
                    duration: 2000
                })
                let timer = setInterval(() => {
                    countdownTime.value--;
                    if (countdownTime.value < 1) {
                        clearInterval(timer)
                        countdownTime.value = 0
                        isSend.value = false
                    }
                }, 1000)
            } else {
                uni.showToast({
                    title: "账号验证失败",
                    icon: "error",
                    duration: 2000
                })
            }
        })
    }
}

const submit = () => {
    if (checkAdmin.mailCode === '') {
        uni.showToast({
            title: "请填写验证码",
            icon: "error",
            duration: 2000
        })
    } else {
        // checkAdmin.password = md5(checkAdmin.password)
        request.post("/store/loginUser", checkAdmin).then(res => {
            if (res.code === 200) {
                uni.setStorageSync("rsms-token", res.data.token)
                userMain.id = res.data.id
                userMain.tel = res.data.tel
                userMain.mail = res.data.mail
                userMain.storeCount = res.data.storeCount

                // uni.getStorage({
                //     key: "openId",
                //     success: res => {
                        userMain.openId = uni.getStorageSync("openId")
                        console.log(userMain)
                        request.post("/store/update/bindOpenId", userMain).then(res => {
                            if (res.code===200)
                                uni.showToast({
                                    title: "微信已绑定",
                                    duration: 2000
                                })
                            else
                                uni.showToast({
                                    title: "微信绑定未成功",
                                    duration: 2000
                                })
                        })
                //     }
                // })
                uni.setStorageSync("userMain", userMain)
                uni.reLaunch({
                    url: "/pages/home/index"
                })


            } else {
                uni.showToast({
                    title: res.message,
                    icon: "error",
                    duration: 2000
                })
            }
        })
    }


}


</script>

<style scoped>

</style>